package com.lbssoft.gpsbd.dao;

import com.lbssoft.gpsbd.dao.sql.DeviceSql;
import com.lbssoft.gpsbd.dao.sql.PermissionSql;
import com.lbssoft.gpsbd.model.DeviceType;
import com.lbssoft.gpsbd.model.Permission;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;
import java.util.Set;

@Mapper
public interface PermissionMapper {
	
	/**
	 * 根据用户id获取用户权限
	 * @return
	 */
	@Select("SELECT b.* FROM lbs_user_role t LEFT JOIN lbs_role_permission a ON a.roleId = t.roleId LEFT JOIN lbs_permissions b ON b.id = a.permissionId WHERE t.userId = #{uid}")
	List<Permission> getPermissionsByUid(@Param("uid") long uid);

	/**
	 * 根据角色获取权限
	 * @param roleId
	 * @return
     */
	@Select("SELECT * FROM lbs_permissions t LEFT JOIN lbs_role_permission a ON a.permissionId = t.id  WHERE a.roleId = #{roleId}")
	List<Permission> getPermissionsByRoleId(@Param("roleId") int roleId);


	/**
	 * 管理获取菜单
	 * @param roleId
	 * @return
	 */
	@Select("SELECT * FROM lbs_permissions t LEFT JOIN lbs_role_permission a ON a.permissionId = t.id  WHERE a.roleId = #{roleId} and groupId = #{groupId} and type=0 ORDER BY t.sort")
	List<Permission> getMenusByRoleId(@Param("roleId") int roleId,@Param("groupId") int groupId);



	/**
	 * 获取权限资源
	 * @return
     */
	@Select("SELECT * FROM lbs_permissions t WHERE t.groupId = #{groupId} ORDER BY sort")
	List<Permission> list(@Param("groupId") int groupId);

	/**
	 * 添加资源
	 * @param permis
     */
	@Insert("INSERT INTO lbs_permissions(parentId,name,permis,type,groupId,component,path,icon) VALUE(#{parentId},#{name},#{permis},#{type},#{groupId},#{component},#{path},#{icon})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void add(Permission permis);

	/**
	 * 删除资源
	 * @param id
     */
	@Delete("DELETE FROM lbs_permissions where id = #{id}")
	void delete(@Param("id") int id);

	/**
	 * 根据角色获取权限ids
	 * @return
	 */
	@Select("SELECT id FROM lbs_permissions t LEFT JOIN lbs_role_permission a ON a.permissionId = t.id  WHERE a.roleId = #{roleId} and type in(${types})")
	Set<Integer> getPermissionIds(@Param("roleId") int roleId,@Param("types") String types);

	/**
	 * 修改权限信息
	 * @param permis
     */
	//@Update("UPDATE lbs_permissions SET name = #{name},permis=#{permis},parentId=#{parentId},component=#{component},icon=#{icon},path=#{path} where id = #{id}")
	@UpdateProvider(type = PermissionSql.class, method = "update")
	void update(Permission permis);

	/**
	 * 根据父id获取子权限列表
	 * @param parentId
	 * @return
     */
	@Select("SELECT * FROM lbs_permissions WHERE parentId = #{parentId}")
	List<Permission> getPermissionsByPid(@Param("parentId") int parentId);

	/**
	 * 根据权限id获取兄弟权限集合
	 * @param id
	 * @return
     */
	@Select("SELECT * FROM lbs_permissions where  parentId in(SELECT parentId FROM lbs_permissions where id = #{id}) and groupId = (SELECT groupId FROM lbs_permissions where id = #{id}) ORDER BY sort")
	List<Permission> getBrothersPermisById(@Param("id") int id);

	/**
	 * 置换2个权限的排序位置
	 * @param id
	 * @param targetId
     */
	@Update("UPDATE lbs_permissions AS g1 JOIN lbs_permissions AS g2 ON (g1.id = #{id} AND g2.id = #{targetId}) OR (g1.id = #{targetId} AND g2.id = #{id}) SET g1.sort = g2.sort, g2.sort = g1.sort")
	void  displaceSort(@Param("id") int id,@Param("targetId") int targetId);

	/**
	 * 添加资源后修改资源的排序
	 * @param id
	 * @param sort
     */
	@Update("UPDATE lbs_permissions SET sort = #{sort} where id = #{id}")
	void updateSort(@Param("id") int id,@Param("sort") int sort);

	
}
